package com.hnn;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * @author hnn
 * @version 1.0
 * @date 2023-12-18 18:30
 */
public class PortMap extends Thread {

//    private String newhost;
//    private int newport;
//    private int port;
//    private String local_ip;
//    private Socket sock;
//
//    public PortMap(int port,String newhost,int newport){
//        this.port=port;
//        this.newhost=newhost;
//        this.newport=newport;
//        this.sock=new Socket()
//    }

    public static void startPort(final int localPort,final String remoteHost,final int remotePort) throws IOException {
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 创建本地服务器套接字
                    ServerSocket ss = new ServerSocket(localPort);
                    System.out.println("等待客户端连接...");

                    while (true) {
                        // 监听客户端连接
                        Socket clientSocket = ss.accept();
                        System.out.println("客户端连接成功!");

                        // 创建与远程主机的套接字连接
                        Socket remoteSocket = new Socket(remoteHost, remotePort);

                        // 创建线程进行数据转发
                        Thread inputThread = new Thread(new ForwardingHandler(clientSocket.getInputStream(), remoteSocket.getOutputStream()));
                        Thread outputThread = new Thread(new ForwardingHandler(remoteSocket.getInputStream(), clientSocket.getOutputStream()));

                        inputThread.start();
                        outputThread.start();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        t.setDaemon(true);
        t.start();

    }

    // 数据转发处理器
    static class ForwardingHandler implements Runnable {
        private final InputStream input;
        private final OutputStream output;

        public ForwardingHandler(InputStream input, OutputStream output) {
            this.input = input;
            this.output = output;
        }

        @Override
        public void run() {
            byte[] buffer = new byte[4096];
            int bytesRead;

            try {
                while ((bytesRead = input.read(buffer)) != -1) {
                    output.write(buffer, 0, bytesRead);
                    output.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    input.close();
                } catch (IOException e) {

                }
                try {
                    output.close();
                } catch (IOException e) {

                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        String remort_host="192.168.20.231";
        PortMap.startPort(12345,remort_host,8000);
        PortMap.startPort(8200,remort_host,8200);
        PortMap.startPort(554,remort_host,554);
        PortMap.startPort(8117,remort_host,8117);
        PortMap.startPort(8080,remort_host,8080);

        Thread.sleep(15555555);
    }
}
